﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scenario.WorkflowEngine.DataModel.WorkflowDataTypes;

namespace Scenario.WAPI
{
    public partial class DataTypesHelper
    {
        public DataTypesHelper()
        {
        }

        public XMLDataTypes GetDataTypes()
        {
            Response wapiResponse = new Response();

            String sXML = DataTypesHelper.GetDataTypes(ref wapiResponse);
            if (wapiResponse.iCode != 0)
            {
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
            }
            XMLDataTypes xmlDatatypes = new XMLDataTypes();
            xmlDatatypes.Parse (sXML);

            return xmlDatatypes;

        }

        /// <summary>
		/// Gets the values.
		/// </summary>
		/// <param name="sDataType2Search">The s data type2 search.</param>
		/// <returns></returns>
        public XMLEnumerationValues GetValues(string sDataType2Search)
		{
			Response wapiResponse = new Response ();
			string sDataType = DataTypesHelper.GetDataTypes (sDataType2Search, ref wapiResponse);
			if ( wapiResponse.iCode == 0 )
			{
				XMLDataType datatype = new XMLDataType ();
				datatype.Parse (sDataType);

				if ( datatype.XMLEnumerationValues.Count == 0 ) 
				{
					/*XMLEnumerationValues vValues = new XMLEnumerationValues();
					try
					{
						BasicBusinessObjectCollection vRecords = null;
						vRecords = PCMDataBase.Instance().GetBusinessObjectRecords (sDataType2Search, null);
						if (vRecords != null) 
						{
							for ( int i=0; i<vRecords.Count; i++)
							{
								BasicBusinessObject dummyObj = vRecords[i];
								XMLDataTypeValue vValue = new XMLDataTypeValue();
								vValue.Key = dummyObj.Id.ToString();
								vValue.Value = dummyObj.Item;

								vValues.Add ( vValue);
							}
						}
					}
					catch ( DBAdapterException )
					{
					}
					catch
					{
					}

					return vValues;
                     * */
				}

				return datatype.XMLEnumerationValues;
			}
			else
			{
                throw new Exception.WAPIException();
			}
		}

        public string GetValue(string sDataType, string sKey)
		{
			XMLEnumerationValues vValues = this.GetValues(sDataType);
			XMLDataTypeValue xmlValue = vValues.GetValue (sKey);

			if ( xmlValue == null )
			{
                throw new Exception.WAPIException();
			}
			else
			{
				return xmlValue.Value;
			}

			/*
			 * Response wapiResponse = new Response ();
			string sXML = WAPI.DataTypes.GetDataTypes (sDataType, ref wapiResponse);
			if ( wapiResponse.iCode == 0 )
			{
				XMLDataType datatype = new XMLDataType ();
				datatype.Parse (sXML);

				XMLDataTypeValue xmlValue = datatype.XMLEnumerationValues.GetValue (sKey);

				if ( xmlValue == null )
				{
					throw new DataTypeException();
				}
				else
				{
					return xmlValue.Value;
				}
			}
			else
			{
				throw new DataTypeException();
			}
			*/
		}
    }
}
